<?php
namespace app\api\service\uniapp\goods;

use app\api\model\Region;
use app\api\model\ShopYiwu;
use app\api\model\ShopYiwuCart;
use app\api\model\UserStore;
use app\common\controller\Api;
use think\Db;

class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getGoodsByKey($key){
        $goods = ShopYiwu::where(['title'=>array('like','%'.$key.'%'),'is_check'=>1,'is_on_sale'=>1])->select();
        foreach ($goods as $k=>$v){
            //判断是否有缩图
            if(empty($v['thumb_pic']) || strstr($v['thumb_pic'],'/thumb/')===false){
                $thumbimg = get_thumb_image(unserialize($v['main_pics'])[0],0,300);
                //halt($thumbimg);
                $v['thumb_pic'] = $thumbimg;
                ShopYiwu::where('id',$v['id'])->update(['thumb_pic'=>'/public'.$thumbimg]);
            }
            $goods[$k]['thumb_pic'] = config('img_url').$v['thumb_pic'];
        }
        return $goods;
    }
    public function loadGoodsCart($user_id,$shop_id){
        //获取店铺下所有已审核上架中的商品
        $goods_ids = ShopYiwu::where(['shop_id'=>$shop_id,'is_check'=>1,'is_on_sale'=>1])->column('id');
        //获取用户在该店铺的购物车数据
        $cart = ShopYiwuCart::where(['user_id'=>$user_id,'goods_id'=>array('in',$goods_ids)])->select();
        $total_num = 0;
        $total_price = 0;
        foreach ($cart as $k=>$v){
            $total_num+=$v['goods_num'];
            $total_price+=$v['total_price'];
        }
        $cartdata['total_num'] = $total_num;
        $cartdata['total_price'] = $total_price;
        $cartdata['cart'] = $cart;
        return $cartdata;
    }
}